package com.parenting.oauth2.service;




import com.parenting.feign.controller.IFeignSystemShopController;
import com.parenting.feign.controller.IFeignSystemUserController;
import com.parenting.feign.entities.ParentingShopDTO;
import com.parenting.model.User;
import com.parenting.oauth2.exception.ValidateCodeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component("customUserDetailsService") // 不一定不要少了
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private IFeignSystemUserController feignSystemUserController;

    @Autowired
    private IFeignSystemShopController feignSystemShopController;

     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1. 判断用户名是否为空
        if(StringUtils.isEmpty(username)) {
            log.info("================用户名不能为空=================");
            throw new ValidateCodeException("用户名不能为空");
        }
         User sysUser = feignSystemUserController.getUserByAccount(username);

         log.info("UserDTO {}",sysUser.getId());
        // 2. 通过用户名查询数据库中的用户信息
        if(sysUser == null) {
            log.info("================用户名或密码错误=================");
            throw new ValidateCodeException("用户名或密码错误");
        }
        List<GrantedAuthority> authorities=null;
        // 3. 通过用户id去查询数据库的拥有的权限信息
         log.info("getTeacherId {}",sysUser.getId());
        List<String> authorityCode = feignSystemUserController.findCodeByUserId(sysUser.getId());
         log.info("authorityCode {}",authorityCode);
        // 4. 封装权限信息（权限标识符code）
        if(CollectionUtils.isNotEmpty(authorityCode)) {
            authorities = new ArrayList<>();
            for(String code: authorityCode) {
                if (StringUtils.isNotEmpty(code)){
                    authorities.add(new SimpleGrantedAuthority(code));
                }
            }

            ParentingShopDTO shopByShopId=null;
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(sysUser.getShopId())){
                shopByShopId = feignSystemShopController.getShopByShopsId(sysUser.getShopId());
            }

            // 5. 构建UserDetails接口的实现类JwtUser对象
            JwtUser jwtUser = new JwtUser( sysUser.getId(), sysUser.getFullName(),sysUser.getAccount(), sysUser.getPassword(),
                    sysUser.getFullName(), sysUser.getImageUrl(), sysUser.getMobile(), "",
                    sysUser.getIsAccountNonExpired(), sysUser.getIsAccountNonLocked(),
                    sysUser.getIsCredentialsNonExpired(), sysUser.getIsEnabled(),"",""
                    ,sysUser.getShopId(),null==shopByShopId?"":shopByShopId.getShopName(), authorities,null==shopByShopId?6:shopByShopId.getState());
            return jwtUser;
        }else{
            log.info("================无权限登录该系统=================");
            throw new ValidateCodeException("无权限登录该系统！");
        }

    }
}
